package wyu.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dto.Result;
import com.entity.UserBarNum;
import org.springframework.stereotype.Service;
import wyu.mapper.UserBarMapper;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserBarServiceImpl extends ServiceImpl<UserBarMapper, UserBarNum> implements UserBarService {
    @Resource
    UserBarMapper userBarMapper;
    @Override
    public int getUserBarCount(int barId) {
        Integer count = query().eq("BAR_ID", barId).count();
        return count;
    }

    @Override
    public List<Integer> getUserBarList(String uname) {
        return userBarMapper.getUserBarList(uname);
    }

    @Override
    public boolean joinBar(int barId, String uName) {
        Integer count = query().eq("BAR_ID", barId).eq("USER_UNAME", uName).count();
        if (count==0){
            //未关注该贴吧
            UserBarNum userBarNum = new UserBarNum();
            userBarNum.setBarId(barId);
            userBarNum.setUserUname(uName);
            boolean save = save(userBarNum);
            return save;
        }
        return false;
    }

    @Override
    public boolean exitBar(int barId, String uName) {
        Map<String,Object> map = new HashMap<>();
        map.put("USER_UNAME",uName);
        map.put("BAR_ID",barId);
        int i = userBarMapper.deleteByMap(map);
        if (i==1){
            return true;
        }
        return false;
    }
}
